package com.es.common.aspect;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import com.alibaba.fastjson.JSON;
import com.es.common.annotation.MyAnnotation;
import com.es.web.base.view.BaseView;


@Component
public class LoginInterceptor implements HandlerInterceptor{
	
	
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		HandlerMethod method = (HandlerMethod) handler;
		MyAnnotation myAnnotation = method.getMethodAnnotation(MyAnnotation.class);
		//myAnnotation.params()
		String url = request.getRequestURI();
		if (url.endsWith("/info")) {
			HttpSession session = request.getSession();
			Object obj = session.getAttribute(session.getId());
			if (obj == null) {
				PrintWriter out = response.getWriter();
				BaseView view = new BaseView(500, "未登录");
				String result = JSON.toJSONString(view);
				out.write(result);
				out.flush();
				out.close();
				return false;
			}
		}
		return true;
	}
	
	
}
